package guda.house.web.action.api;

import guda.grape.autogen.common.page.BizResult;
import guda.grape.util.RequestUtil;
import guda.house.biz.BuildingBiz;
import guda.house.biz.SuiteBiz;
import guda.house.biz.UnitBiz;
import guda.house.biz.VillageBiz;
import guda.house.biz.beans.vo.JsonResult;
import guda.house.biz.query.BuildingQuery;
import guda.house.biz.query.SuiteQuery;
import guda.house.biz.query.UnitQuery;
import guda.house.biz.query.VillageQuery;
import guda.house.biz.util.ErrorCode;
import guda.house.web.helper.ResponseHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by well on 16/3/4.
 */
@Controller
@RequestMapping(value = "api/village")
public class ApiVillageAction {

    @Autowired
    private VillageBiz villageBiz;
    @Autowired
    private BuildingBiz buildingBiz;
    @Autowired
    private UnitBiz unitBiz;
    @Autowired
    private SuiteBiz suiteBiz;

    @RequestMapping(value = "getVillage.htm")
    public void getVillage(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        JsonResult result = JsonResult.getInstance();
        VillageQuery villageQuery = new VillageQuery();
        villageQuery.setPageSize(10000);
        //modelMap.put("query", villageQuery);
        BizResult bizResult = villageBiz.list(villageQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }

    @RequestMapping(value = "getBuilding.htm")
    public void getBuilding(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long villageId = RequestUtil.getLong(request, "villageId");
        JsonResult result = JsonResult.getInstance();
        if(villageId == 0){
            result.setErrMsg("缺少参数!");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }

        BuildingQuery buildingQuery = new BuildingQuery();
        buildingQuery.setPageSize(10000);
        buildingQuery.setVillageId(villageId);
        //modelMap.put("query", buildingQuery);
        BizResult bizResult = buildingBiz.list(buildingQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }

    @RequestMapping(value = "getUnit.htm")
    public void getUnit(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long buildingId = RequestUtil.getLong(request, "buildingId");
        JsonResult result = JsonResult.getInstance();
        if(buildingId == 0){
            result.setErrMsg("缺少参数!");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        UnitQuery unitQuery = new UnitQuery();
        unitQuery.setPageSize(10000);
        unitQuery.setBuildingId(buildingId);
        //modelMap.put("query", buildingQuery);
        BizResult bizResult = unitBiz.list(unitQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }

    @RequestMapping(value = "getSuite.htm")
    public void getSuite(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        long unitId = RequestUtil.getLong(request, "unitId");
        JsonResult result = JsonResult.getInstance();
        if(unitId == 0){
            result.setErrMsg("缺少参数!");
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
        SuiteQuery suiteQuery = new SuiteQuery();
        suiteQuery.setPageSize(10000);
        suiteQuery.setUnitId(unitId);
        //modelMap.put("query", buildingQuery);
        BizResult bizResult = suiteBiz.list(suiteQuery);
        if (bizResult.success) {
            result.getMap().putAll(bizResult.data);
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        } else {
            result.setErrMsg(ErrorCode.getMessage(bizResult.code));
            ResponseHelper.ajaxReturn(result.getMap(), response);
            return;
        }
    }
}
